//
//  WKUsernameLoginModule.m
//  WuKongUsernameLogin
//
//  Created by tt on 2023/9/7.
//

#import "WKUsernameLoginModule.h"
#import <WuKongUsernameLogin/WuKongUsernameLogin-Swift.h>
#import "WKUsernameLoginVC.h"
#import <IQKeyboardManager/IQKeyboardManager.h>
//#import "WKForgetPasswordVC.h"
#import "WKForgetPasswordVC.h"
#import "WKUsernameLoginVC.h"
#import "WKLoginVC.h"

@WKModule(WKUsernameLoginModule)
@implementation WKUsernameLoginModule


+(NSString*) gModuleID {
    return @"WuKongUsernameLogin";
}

-(NSString*) moduleId {
    return [[self class] gModuleID];
}


- (void)moduleInit:(WKModuleContext*)context{
    NSLog(@"【WuKongUsernameLogin】模块初始化！");
    
    
  
//    NSString *mnemonics = [WKWeb3Helper generateMnemonics];
//    
//    [WKWeb3Helper createUserWallet:mnemonics uid:@"zk1234512"];
//    
//    NSString *privateKey = [WKWeb3Helper getUserPrivateKey:@"zk1234512"];
//    
//   NSString *publicKey = [WKWeb3Helper privateKeyToPublicKey:privateKey];
    
    // 显示登录页面
    [self setMethod:WKPOINT_LOGIN_SHOW handler:^id _Nullable(id  _Nonnull param) {
        WKLoginVC *loginVC = [WKLoginVC new];
//        UserLoginVC *loginVC = [UserLoginVC new]; // 第三方授权登录UI
        [[WKNavigationManager shared] resetRootViewController:loginVC];
        return nil;
    }];
    
}


- (BOOL)moduleDidFinishLaunching:(WKModuleContext *)context {
   
    IQKeyboardManager.sharedManager.enable = NO;
    [[IQKeyboardManager sharedManager].enabledDistanceHandlingClasses addObject:WKForgetPasswordVC.class];
    [[IQKeyboardManager sharedManager].enabledDistanceHandlingClasses addObject:WKUsernameLoginVC.class];
    [[IQKeyboardManager sharedManager] setEnableAutoToolbar:NO];
    
    NSString *finishedWeb3Veritfy = WKApp.shared.loginInfo.extra[@"finishedWeb3Veritfy"];
    if(!finishedWeb3Veritfy || ![finishedWeb3Veritfy isEqualToString:@"1"]) {
        [WKApp.shared logout];
        return YES;
    }
    return YES;
}

- (NSMutableString *)dataToHexString:(NSData *)valueData insertSql:(NSMutableString *)insertSql {
    if (@available(iOS 13, *)) {
        if ([valueData isKindOfClass:[NSData class]]) {
            NSMutableString *valueString = [NSMutableString string];
            const char *bytes = valueData.bytes;
            NSInteger count = valueData.length;
            for (int i = 0; i < count; i++) {
                [valueString appendFormat:@"%02x", bytes[i]&0x000000FF];
            }
            
            [insertSql appendFormat:@"'%@',", [valueString stringByReplacingOccurrencesOfString:@"'" withString:@"''"]];
        } else {
            [insertSql appendFormat:@"'%@',", [[NSString stringWithFormat:@"%@", valueData] stringByReplacingOccurrencesOfString:@"'" withString:@"''"]];
        }
    } else {
        [insertSql appendFormat:@"'%@',", [[NSString stringWithFormat:@"%@", valueData] stringByReplacingOccurrencesOfString:@"'" withString:@"''"]];
    }
    return insertSql;
}

@end
